﻿using System;
using NHibernate.Dialect;

namespace HYCodeBase.SqlUtility.ORMSuit
{
	public class MysqlConfigurationPacker : ConfigurationPacker
	{
		public MysqlConfigurationPacker(string inputDbName, string inputDbIP, string inputUserName, string inputPassWord = null, string inputPort = "3306")
		{
			this.m_useDatabaseName = inputDbName;
			this.m_useDbIP = inputDbIP;
			this.m_usePort = inputPort;
			this.m_usePassWord = inputPassWord;
			this.m_useRootName = inputUserName;
			this.m_strUseDirverFileName = "MySql.Data.dll";
			this.m_dicUseProperties.Add("connection.driver_class", "NHibernate.Driver.MySqlDataDriver");
			this.m_dicUseProperties.Add("dialect", "NHibernate.Dialect.MySQLDialect");
			this.m_dicUseProperties.Add("hbm2ddl.keywords", "none");
		}

		protected override string MakeConnectionString()
		{
			string result = string.Empty;
			if (!string.IsNullOrWhiteSpace(this.m_usePassWord) && !string.IsNullOrWhiteSpace(this.m_useRootName))
			{
				result = string.Concat(new string[]
				{
					"Database=",
					this.m_useDatabaseName,
					";Data Source=",
					this.m_useDbIP,
					";User Id=",
					this.m_useRootName,
					";Password=",
					this.m_usePassWord,
					";port=",
					this.m_usePort,
					";pooling=false;CharSet=utf8"
				});
			}
			else if (!string.IsNullOrWhiteSpace(this.m_useRootName) && string.IsNullOrWhiteSpace(this.m_usePassWord))
			{
				result = string.Concat(new string[]
				{
					"Database=",
					this.m_useDatabaseName,
					";Data Source=",
					this.m_useDbIP,
					";User Id=",
					this.m_useRootName
				});
			}
			return result;
		}

		internal override Dialect GetUseDialect()
		{
			return new SQLiteDialect();
		}

		private string m_useDatabaseName;

		private string m_useDbIP;

		private string m_usePort;

		private const string DEFAULTPORT = "3306";
	}
}
